﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Threading;

namespace TD
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class TDGame : Microsoft.Xna.Framework.Game
    {
        /// <summary>
        /// 防御塔帮助类
        /// </summary>
        StockadeHelper stockadeHelper;
        /// <summary>
        /// 怪物帮助类
        /// </summary>
        MonsterHelper monsterHelper;


        MessageBarHelper messageBarHelper;

        GraphicsDeviceManager graphics;
        public static SpriteBatch spriteBatch;



        /// <summary>
        /// 是否在暂停状态
        /// </summary>
        public static bool IsPaused = false;
        /// <summary>
        /// 是否已经失败
        /// </summary>
        public static bool IsLose = false;

        /// <summary>
        /// 默认字体
        /// </summary>
        public static SpriteFont DefaultFont;

        /// <summary>
        /// 用于提示的大字体
        /// </summary>
        public static SpriteFont BigFont;
        /// <summary>
        /// 敌人的列表
        /// </summary>
        public static List<Monster> Monsters = new List<Monster>();

        /// <summary>
        /// 最大允许的怪物到达终点数目
        /// </summary>
        public int MaxMonsterGoEndCount = 5;

        /// <summary>
        /// 信息框位置
        /// </summary>
        public Vector2 MessageVector;


        /// <summary>
        /// 防御塔
        /// </summary>
        public static List<Stockade> Stockades = new List<Stockade>();


        StockadeToolBar stockadeToolBar;
        public TDGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";




        }

        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                // TODO: Load any ResourceManagementMode.Automatic content
                this.mousePointer.PointerTexture = Content.Load<Texture2D>(@"Mouse\Default");
            }

            base.LoadGraphicsContent(loadAllContent);
        }
        void monsterHelper_MonsterGoEnd(Monster monster)
        {
            int currentCount = Interlocked.Decrement(ref MaxMonsterGoEndCount);
            if (currentCount == 0)//you lose
            {
                IsLose = true;

            }

        }
        MousePointer mousePointer;
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            mousePointer = new TD.MousePointer(this);
            mousePointer.DrawOrder = 1000;
            mousePointer.RestrictZone = new Rectangle(0, 0,
                 graphics.GraphicsDevice.Viewport.Width,
                 graphics.GraphicsDevice.Viewport.Height);
            mousePointer.OnClick += new ButtonClick(mousePointer_OnClick);
            Components.Add(mousePointer);


            stockadeHelper = new StockadeHelper(this);
            Components.Add(stockadeHelper);

            monsterHelper = new MonsterHelper(this);
            monsterHelper.MonsterGoEnd += new MonsterDelegate(monsterHelper_MonsterGoEnd);
            Components.Add(monsterHelper);

            messageBarHelper = new MessageBarHelper(this);
            Components.Add(messageBarHelper);



            

            stockadeToolBar = new StockadeToolBar(this);
            Components.Add(stockadeToolBar);
            base.Initialize();
        }

        void mousePointer_OnClick(ButtonType button, Vector2 point)
        {
            //   IsPaused = !IsPaused;

        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            DefaultFont = Content.Load<SpriteFont>("宋体");
            BigFont = Content.Load<SpriteFont>("Courier New");

            MessageVector.X = Window.ClientBounds.Width / 2 - 50;
            MessageVector.Y = Window.ClientBounds.Height / 2 - 30;

            //    stockadeHelper.LoadContent(Content);
            //    monsterHelper.LoadContent(Content);

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            // stockadeHelper.Update(gameTime);
            //  monsterHelper.Update(gameTime);


            KeyboardState keyboardState = Keyboard.GetState();
            if (keyboardState.GetPressedKeys().Length > 1)
            {

            }
            if (keyboardState.IsKeyDown(Keys.Space))
            {
                BeignChange = true;

            }
            if (BeignChange)
            {
                if (keyboardState.IsKeyUp(Keys.Space))
                {
                    IsPaused = !IsPaused;
                    BeignChange = false;
                }
            }



          
            base.Update(gameTime);
        }


        private bool BeginLeftButton = false;
        private bool BeignChange = false;
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);


            //  stockadeHelper.Draw(gameTime);
            //   monsterHelper.Draw(gameTime);

            if (IsLose)
            {
                spriteBatch.Begin();
                spriteBatch.DrawString(BigFont, "Over !!!", MessageVector, Color.Red);
                spriteBatch.End();
            }

            // TODO: Add your drawing code here

            spriteBatch.Begin();

            if (IsPaused)
            {
                spriteBatch.DrawString(BigFont, "Paused !!!", MessageVector, Color.Red);
            }


            spriteBatch.DrawString(DefaultFont, "Last:" + MaxMonsterGoEndCount, new Vector2(Window.ClientBounds.Width - 100, 20), Color.Red);


            spriteBatch.End();




            base.Draw(gameTime);
        }
    }
}
